home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / muds / mordor_2.000 / mordor_2 / src / auth.c < prev    next >
C/C++ Source or Header  |  1994-10-03  |  5KB  |  221 lines

  1. /*
  2.  * AUTH.C:
  3.  *
  4.  *    Performs user authentication services.  Retrieves the
  5.  *    host name of the user.  Also retrieves the user id if
  6.  *    the host is running identd.
  7.  *
  8.  *    Copyright (C) 1994   Brett J. Vickers
  9.  *
  10.  */
  11.  
  12. #include <sys/types.h>
  13. #include <sys/socket.h>
  14. #include <sys/ioctl.h>
  15. #include <netinet/in.h>
  16. #include <arpa/inet.h>
  17. #include <sys/time.h>
  18. #include <netdb.h>
  19. #include <errno.h>
  20. #include <stdio.h>
  21. #include <ctype.h>
  22. #include <sys/signal.h>
  23. #define MIGNORE
  24. #include "mstruct.h"
  25. #include "mextern.h"
  26. #include "strstr.c"
  27.  
  28. char Output[2][80];
  29. char *start_auth();
  30. void parse();
  31. int abort();
  32.  
  33. main(argc, argv)
  34. int argc;
  35. char *argv[];
  36. {
  37.     FILE *fp;
  38.     char ipaddr[80], filename[127];
  39.     int oport, iport;
  40.     char buf[80];
  41.     struct hostent *h;
  42.     struct in_addr in;
  43.     struct itimerval timer_value;
  44.  
  45.     timer_value.it_interval.tv_sec = 0L;
  46.     timer_value.it_interval.tv_usec = 0L;
  47.     timer_value.it_value.tv_sec = 20L;
  48.     timer_value.it_value.tv_usec = 0L;
  49.  
  50.     setitimer(ITIMER_REAL, &timer_value, 0);
  51.  
  52.     signal(SIGALRM, abort);
  53.  
  54.     if(argc != 4) {
  55.         fprintf(stderr, "Syntax: %s ip_address oport iport\n", argv[0]);
  56.         exit(0);
  57.     }
  58.  
  59.     strcpy(ipaddr, argv[1]);
  60.     oport = atoi(argv[2]);
  61.     iport = atoi(argv[3]);
  62.     in.s_addr = inet_addr(ipaddr);
  63.  
  64.     /* Perform the host name lookup on the IP address*/
  65.     h = gethostbyaddr((char *)&in, sizeof(in), AF_INET);
  66.     if(!h)
  67.         strcpy(Output[1], "UNKNOWN");
  68.     else
  69.         strcpy(Output[1], h->h_name);
  70.  
  71.     /* Begin the user id lookup */
  72.     if(start_auth(&in, oport, iport, buf))
  73.         strcpy(Output[0], buf);
  74.     else
  75.         strcpy(Output[0], "no_port");
  76.  
  77.     /* Save the results to a file so frp can look them up */
  78.     sprintf(filename, "%s/auth/lookup.%d", LOGPATH, getpid());
  79.     fp = fopen(filename, "w");
  80.     if(!fp) exit(-1);
  81.     fprintf(fp, "%s\n%s\n", Output[0], Output[1]);
  82.     fclose(fp);
  83.  
  84.     exit(0);
  85. }
  86.  
  87. /************************************************************************/
  88. /*                start_auth                */
  89. /************************************************************************/
  90.  
  91. /* This function opens a socket to the identd port of the player's    */
  92. /* machine (port 113) and requests the user id of that player.        */
  93.  
  94. char *start_auth(in, oport, iport, buf)
  95. struct in_addr *in;
  96. int oport, iport;
  97. char *buf;
  98. {
  99.     struct sockaddr_in sin;
  100.     int fd, i, n, Tablesize, len;
  101.     fd_set Sockets, sockcheck;
  102.     struct timeval t;
  103.     char outstr[80];
  104.  
  105.     t.tv_sec = 10L;
  106.     t.tv_usec = 0L;
  107.  
  108.     sin.sin_addr.s_addr = in->s_addr;
  109.     sin.sin_family = AF_INET;
  110.     sin.sin_port = htons(113);
  111.  
  112.     fd = socket(AF_INET, SOCK_STREAM, 0);
  113.     if(fd < 0) return((char *)0);
  114.  
  115.     if(connect(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0)
  116.         return((char *)0);
  117.  
  118.     FD_ZERO(&Sockets);
  119.     FD_SET(fd, &Sockets);
  120.  
  121.     /* Generate request string */
  122.     sprintf(outstr, "%d , %d\r\n", oport, iport);
  123.     len = strlen(outstr);
  124.  
  125.     Tablesize = getdtablesize();
  126.     sockcheck = Sockets;
  127.  
  128.     /* Wait until we can send our request */
  129.     if(select(Tablesize, 0, &sockcheck, 0, &t) <= 0)
  130.         return((char *)0);
  131.  
  132.     /* Send the request */
  133.     n = write(fd, outstr, len);
  134.     if(n < len) return((char *)0);
  135.  
  136.     /* Wait until the other side responds */
  137.     sockcheck = Sockets;
  138.     if(select(Tablesize, &sockcheck, 0, 0, &t) <= 0)
  139.         return((char *)0);
  140.  
  141.     /* Read the response */
  142.     n = read(fd, buf, 80);
  143.     close(fd);
  144.     if(n <= 0)
  145.         return((char *)0);
  146.     else {
  147.         buf[n-1] = 0;
  148.         parse(buf);
  149.         return(buf);
  150.     }
  151. }
  152.  
  153. /************************************************************************/
  154. /*                parse                    */
  155. /************************************************************************/
  156.  
  157. /* This function parses the response from the identity server.  It    */
  158. /* essentially strips out the user id if there is one to be obtained.    */
  159.  
  160. void parse(buf)
  161. char *buf;
  162. {
  163.     char *p, temp[80];
  164.  
  165.     if(p = (char *)strstr(buf, "USERID")) {
  166.         while(*p && *p != ':') p++; p++;
  167.         while(*p && *p != ':') p++;
  168.         while(*p &&
  169.               !(*p <= '9' && *p >= '0') &&
  170.               !(*p <= 'z' && *p >= 'a') &&
  171.               !(*p <= 'Z' && *p >= 'A')) p++;
  172.         strcpy(temp, p);
  173.         p = temp;
  174.         while(*p &&
  175.             ((*p >= 'a' && *p <='z') ||
  176.              (*p >= 'A' && *p <='Z') ||
  177.                  (*p >= '0' && *p <= '9'))) p++;
  178.         *p = 0;
  179.         strcpy(buf, temp);
  180.         buf[8] = 0;
  181.     }
  182.     else {
  183.         strcpy(buf, "unknown");
  184.     }
  185.  
  186. }
  187.  
  188. /************************************************************************/
  189. /*                logf                    */
  190. /************************************************************************/
  191.  
  192. /* Log debugging information if necessary.                */
  193.  
  194. void logf(fmt, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10)
  195. char    *fmt;
  196. int     i1, i2, i3, i4, i5, i6, i7, i8, i9, i10;
  197. {
  198.         char    file[80];
  199.         char    str[1024];
  200.         int     fd;
  201.  
  202.         sprintf(file, "%s/authlog", LOGPATH);
  203.         fd = open(file, O_RDWR, 0);
  204.         if(fd < 0) {
  205.                 fd = open(file, O_RDWR | O_CREAT, ACC);
  206.                 if(fd < 0) return;
  207.         }
  208.         lseek(fd, 0L, 2);
  209.  
  210.         sprintf(str, fmt, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10);
  211.  
  212.         write(fd, str, strlen(str));
  213.  
  214.         close(fd);
  215. }
  216.  
  217. int abort()
  218. {
  219.     exit(0);
  220. }
  221.